package org.example.booksmanagementsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.booksmanagementsystem.constant.Constants;
import org.example.booksmanagementsystem.enums.ResultStatus;
import org.example.booksmanagementsystem.model.*;
import org.example.booksmanagementsystem.service.BookInfoService;
import org.example.booksmanagementsystem.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;



@Slf4j
@RequestMapping("/book")
@RestController
public class BookInfoController {

    @Autowired
    private BookInfoService bookInfoService;
    @Autowired
    private UserInfoService userInfoService;

    // 拦截用户--在interceptor
    @RequestMapping("/getBookInfoListByPage") //前端把值映射到pageRequest的属性上，不调用构造函数
    public Result getBookInfoListByPage(PageRequest pageRequest, HttpSession session){
        log.info("Controller--图书列表展示");

        // 参数校验
        if(pageRequest.getPageSize()<1||pageRequest.getCurrentPage()<=0){

            Result result = new Result();
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("参数验证失败");

            return result;
        }

        PageResult<BookInfo> pageResult = null;

        try{
            pageResult = bookInfoService.getBookInfoListByPage(pageRequest);
        }catch(Exception e){
            log.error("查询翻页信息错误：" + e);
        }

        return Result.success(pageResult);
    }

    // 用户购买图书
    @RequestMapping("/buy")
    public Result buyBook(Integer bookId, HttpSession session){
        log.info("Controller--用户购买图书");

        Result result = new Result();

        //检查参数
        if(bookId==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //调用service进行处理
        boolean b = bookInfoService.buyBook(bookId,userInfo);
        log.info("购买图书的service已调用");
        if(b==true){
            result.setStatus(ResultStatus.SUCCESS);

            log.info("用户{"+userInfo.getId()+"}购买图书的Id{"+bookId+"}");

            return result;
        }else{
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

    }

    // 用户取消购买
    @RequestMapping("/noBuy")
    public Result noBuyBook(Integer bookId, HttpSession session){
        log.info("Controller--用户取消购买图书");

        Result result = new Result();

        //检查参数
        if(bookId==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //调用service进行处理
        boolean b = bookInfoService.noBuyBook(bookId,userInfo);
        log.info("取消购买图书的service已调用");
        if(b==true){
            result.setStatus(ResultStatus.SUCCESS);
            log.info("用户{"+userInfo.getId()+"}取消购买图书的Id{"+bookId+"}");
            return result;
        }else{
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

    }



    // 添加图书
    @RequestMapping("/addBookInfo")
    public String addBookInfo(BookInfo bookInfo){

        //检查参数
        if(bookInfo==null
            || !StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getPrice() ==null
            || bookInfo.getCount()==null
            || bookInfo.getStatus()==null){
            return "输入的参数不合法，请检查后重新输入";
        }

        try{
            bookInfoService.addBookInfo(bookInfo);
        }catch(Exception e){
            log.error("添加图书发生报错e{}："+e);
            return "添加图书失败，请联系管理员";
        }


        return "";
    }

    // 根据Id查询用户
    @RequestMapping("/getBookInfoById")
    public BookInfo getBookInfoById(Integer bookId){ //前端传来的参数是 bookId

        log.info("getBookInfoById：" +"根据Id查询用户信息");
        if(bookId==null || bookId <0){
//            return new BookInfo(); //如果直接返回null，前端在点的时候会发生报错
            return null;
        }

        BookInfo bookInfo = bookInfoService.queryBookInfoById(bookId);

        return bookInfo;
    }

    //根据Id更新信息
    @RequestMapping("/updateBookInfo")
    public String updateBookInfo(BookInfo bookInfo){
        if(bookInfo.getId()==null || bookInfo.getId()<0){
            return "无Id信息，请联系管理员";
        }

        try{
            bookInfoService.updateBookInfo(bookInfo);
            return "";
        }catch(Exception e){
            log.info("Controller-修改信息错误：e{}"+e);
            return "修改失败，请联系管理员";
        }

    }

    // 批量删除
    @RequestMapping("/batchDeleteBookInfoById")
    public String batchDeleteBookInfoById(@RequestParam("idList") List<Integer> idList){
        log.info("controller 接收批量删除请求");
        if(idList == null){
            return "未选择Id，请检查";
        }

        Integer deleteCount = 0;

        try{
            deleteCount = bookInfoService.batchDeleteBookInfoById(idList);

            if(deleteCount !=null){
                return "";
            }else{
                log.info("删除失败，影响的行数为空，请联系管理员");
                return "删除失败";
            }

        }catch(Exception e){
            log.error("Controller 批量删除发生失败:e{}"+e);
            return "发生报错，请联系管理员";
        }

    }

    //批量购买图书
    @RequestMapping("/batchPurchaseBook")
    public Result batchPurchaseBook(@RequestParam("idList") List<Integer> idList, HttpSession session){
        Result result = new Result();

        if(idList == null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        boolean b = bookInfoService.batchPurchaseBook(idList, userInfo);
        if(b!=true){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);
        return result;
    }

    //购物车的图书列表
    @RequestMapping("/getBookInfoListOfNormalUserByPage")
    public Result getBookInfoListOfNormalUserByPage(PageRequest pageRequest, HttpSession session){
        log.info("Controller--用户购物车");

        Result result = new Result();

        // 参数校验
        if(pageRequest.getPageSize()<1||pageRequest.getCurrentPage()<=0){

            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("参数验证失败");

            return result;
        }

        PageResult<BookInfo> pageResult = null;

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        try{
            pageResult = bookInfoService.getBookInfoListOfNormalUserByPage(pageRequest,userInfo);
        }catch(Exception e){
            log.error("查询翻页信息错误：" + e);
        }

        result.setStatus(ResultStatus.SUCCESS);
        result.setData(pageResult);


        return result;
    }

    @RequestMapping("/noBuyOfNormalUser")
    public Result noBuyOfNormalUser(Integer bookId, HttpSession session){
        Result result = new Result();
        if(bookId==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }


        boolean n = bookInfoService.noBuyOfNormalUser(bookId,userInfo.getId());

        if(n !=true){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);

        return result;
    }

    @RequestMapping("/batchNoBuyBook")
    public Result batchNoBuyBook(@RequestParam("idList") List<Integer> idList, HttpSession session){
        Result result = new Result();

        if(idList == null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }


        boolean b = bookInfoService.batchNoBuyBook(idList,userInfo);

        if(b !=true){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);

        return result;
    }

    // 在购物车中购买
    @RequestMapping("/buyOfshoppingTrolly")
    public Result buyOfshoppingTrolly(Integer bookId, HttpSession session){
        Result result = new Result();

        if(bookId==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //从session中提取用户
        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null){
            log.info("提取session中的userInfo失败");
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        boolean b = bookInfoService.buyOfshoppingTrolly(bookId, userInfo);
        if(b==false){

            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);

        return result;
    }


}
